const fs = require('fs');
const path = require('path');
let oldHtml = path.join(__dirname, 'index.html');
let cssIndex = path.join(__dirname, 'abc', 'index.css');
let jsIndex = path.join(__dirname, 'abc', 'index.js');
let newInHtml = path.join(__dirname, 'abc', 'index.html');
fs.readFile(oldHtml, 'utf-8', (err, data) => {
    if (err) console.log(err);
    let cssReg = /<style>([\s\S]*)<\/style>/g;
    let cssResult = cssReg.exec(data);
    fs.writeFile(cssIndex, cssResult[1], (err) => {
        if (err) console.log(err);
        console.log('css分离成功');
    });
    let jsReg = /<script>([\s\S]*)<\/script>/g;
    let jsResult = jsReg.exec(data);
    fs.writeFile(jsIndex, jsResult[1], (err) => {
        if (err) console.log(err);
        console.log('js分离成功');
    });
    data = data.replace(cssReg, '<link rel="stylesheet" href="index.css">');
    data = data.replace(jsReg, '<script src="index.js"></script>');
    fs.writeFile(newInHtml, data, err => {
        if (err) console.log(err);
        console.log('拆分成功');
    });

});